//
//  ModifyNumViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/21.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ModifyNumViewController.h"
#import "LXAlertView.h"
#import "ZJLoginViewController.h"
#import "ZJBaseNavControllerViewController.h"
@interface ModifyNumViewController ()<UITextFieldDelegate>

@property(nonatomic ,strong) UITextField *phoneWord;
@property(nonatomic ,strong) UITextField *passWord1;
@property(nonatomic ,strong) UITextField *file;
@property(nonatomic ,strong) UIButton *GetNumeberBtn;

@end

@implementation ModifyNumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"修改手机号码");
    // Do any additional setup after loading the view.
}

-(void)createView{
    self.phoneWord=[[UITextField alloc]initWithFrame:CGRectMake(10, 30, MainW-20, 49)];
    self.phoneWord.placeholder=MyString(@"请输入新的手机号");
    [self.phoneWord setValue:FontAndStyle14 forKeyPath:@"_placeholderLabel.font"];
    self.phoneWord.textColor=TextColor3;
    self.phoneWord.font=FontAndStyle14;
    self.phoneWord.keyboardType=UIKeyboardTypeNumberPad;
    self.phoneWord.delegate=self;
    self.phoneWord.backgroundColor=[UIColor whiteColor];
    [self.phoneWord addTarget:self action:@selector(controlTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.phoneWord.borderStyle=UITextBorderStyleNone;
    [self.view addSubview:self.phoneWord];

    CGFloat weith=(MainW-25)*9/25;
    self.passWord1=[[UITextField alloc]initWithFrame:CGRectMake(10, 110, MainW-weith-25, 49)];
    self.passWord1.delegate=self;
    self.passWord1.textColor=TextColor3;
    self.passWord1.font=FontAndStyle14;
    self.passWord1.backgroundColor=[UIColor whiteColor];
    self.passWord1.placeholder=MyString(@"请输入验证码");
    [self.passWord1 setValue:FontAndStyle14 forKeyPath:@"_placeholderLabel.font"];
    self.passWord1.keyboardType=UIKeyboardTypeNumberPad;
    self.passWord1.borderStyle=UITextBorderStyleNone;
    [self.view addSubview:self.passWord1];
    [self.passWord1 addTarget:self action:@selector(controlTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.file=[[UITextField alloc]initWithFrame:CGRectMake(MainW-weith-10, 110, weith, 49)];
    self.file.placeholder=MyString(@"获取验证码");
    self.file.enabled=NO;
    self.file.backgroundColor=[UIColor whiteColor];
    [self.file setValue:Red_Color forKeyPath:@"_placeholderLabel.textColor"];
    [self.file setValue:FontAndStyle14 forKeyPath:@"_placeholderLabel.font"];
    self.file.keyboardType=UIKeyboardTypeNumberPad;
    self.file.borderStyle=UITextBorderStyleNone;
    [self.view addSubview:self.file];
    UILabel * placeholderLabel = [self.file valueForKey:@"_placeholderLabel"];
    placeholderLabel.textAlignment = NSTextAlignmentCenter;
    
    self.GetNumeberBtn=[[UIButton alloc]initWithFrame:CGRectMake(MainW-weith-10, 110, weith, 49)];
    [self.GetNumeberBtn addTarget:self action:@selector(GetNumeberBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.GetNumeberBtn.backgroundColor=[UIColor clearColor];
    [self.view addSubview:self.GetNumeberBtn];
    
    UIButton *NextBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 180, MainW-20, 50)];
    [NextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [NextBtn setTitle:MyString(@"下一步") forState:UIControlStateNormal];
    [NextBtn addTarget:self action:@selector(NextBtn) forControlEvents:UIControlEventTouchUpInside];
    [NextBtn setBackgroundColor:Red_Color];
    [self.view addSubview:NextBtn];
    ViewRadius(NextBtn, 5);
    ViewRadius(self.passWord1, 3);
    ViewRadius(self.phoneWord, 3);
    ViewRadius(self.file, 3);
}
-(void)NextBtn{
    if (self.phoneWord.text.length!=0) {
        if (![ZJUITools checkTel:self.phoneWord.text]) {
            [self showToast:MyString(@"请输入正确的手机号码")];
        }else{
            if (self.passWord1.text.length==0) {
                [self showToast:MyString(@"请输入验证码")];
            }else{
                if ([self.passWord1.text isEqualToString:@"123456"]) {
                    LXAlertView*alert=[[LXAlertView alloc]initWithTitle:nil message:MyString(@"设置成功!立即登录") cancelBtnTitle:MyString(@"取消") otherBtnTitle:MyString(@"确定") clickIndexBlock:^(NSInteger clickIndex) {
                        switch (clickIndex) {
                            case 0:
                            {
                                
                            }
                                break;
                            case 1:
                            {
                                ZJLoginViewController *controller = [[ZJLoginViewController alloc] init];
                                ZJBaseNavControllerViewController* nav=[[ZJBaseNavControllerViewController alloc]initWithRootViewController:controller];
                                controller.modality=NO;
                                [self presentViewController:nav animated:YES completion:nil];
                            }
                                break;
                                
                            default:
                                break;
                        }
                    }];
                    [alert showLXAlertView];
                }else{
                    [self showToast:@"验证码错误,请重新输入"];
                }
                
            }
        }
        //[self startTime];
    }else{
        
        [self showToast:MyString(@"请输入手机号码")];
    }
    //NSLog(@"下一步");
}
-(void)GetNumeberBtnClick{
    //NSLog(@"获取验证码");
    if (self.phoneWord.text.length!=0) {
        if (![ZJUITools checkTel:self.phoneWord.text]) {
            [self showToast:MyString(@"请输入正确的手机号码")];
            return;
        }
        NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
        
        [parametersDic setObject:self.phoneWord.text forKey:@"phone"];
        
        [self doHttpRequestWithApi:API_SEND_SMS method:POST dic:parametersDic successBlock:^(NSDictionary *dic) {
            [self showMessage:MyString(@"验证码发送成功，请查收") ];
        }];
        [self startTime];
    }else{
        [self showToast:MyString(@"手机号码不能为空")];
    }
    
}
- (void)startTime
{
    __block int timeout=59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout <= 0)
        { //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.file.placeholder=MyString(@"获取验证码");
                self.phoneWord.enabled=YES;
                self.GetNumeberBtn.userInteractionEnabled = YES;
            });
        }
        else
        {
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.phoneWord.enabled=NO;
                self.file.placeholder=[NSString stringWithFormat:@"%@%@",strTime,MyString(@"秒后重试")];
                self.GetNumeberBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
-(void)controlTextDidChange:(NSNotification *)obj {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(nizana) withObject:obj afterDelay:3.0];
}
-(void)nizana{
    //NSLog(@"监听到了吗");
    [self.phoneWord resignFirstResponder];
    [self.passWord1 resignFirstResponder];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self.phoneWord resignFirstResponder];
    [self.passWord1 resignFirstResponder];
}

@end
